package com.jltx.xq.simplewatch.news.presenter.impl;

import com.jltx.xq.simplewatch.news.model.bean.WeChatNews;
import com.jltx.xq.simplewatch.news.model.impl.WeChatNewsModelImpl;
import com.jltx.xq.simplewatch.news.presenter.IWeChatPresenter;
import com.jltx.xq.simplewatch.news.view.IWeChatNewsView;

import java.util.List;

import cn.jltx.utils.XqLog;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


/**
 * @author jltxseo
 *         Created by junlintianxia on 2017/08/22.
 */

public class WeChatPresenterImpl extends IWeChatPresenter<IWeChatNewsView> {

    private WeChatNewsModelImpl weChatNewsModel;

    public WeChatPresenterImpl(IWeChatNewsView view) {
        super(view);
        weChatNewsModel = new WeChatNewsModelImpl();
    }

    @Override
    public void getWeChatNews(String channel, boolean isDown) {
        mView.showLoadingView(isDown);
        weChatNewsModel.getWeChatNews(channel, 20, isDown)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<WeChatNews>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(List<WeChatNews> weChatNewsList) {
                        XqLog.d(WeChatNewsModelImpl.TAG, "WeChatPresenterImpl.onNext"+Thread.currentThread());
                        if (weChatNewsList != null && weChatNewsList.size() > 0) {
                            for (WeChatNews weChatNews : weChatNewsList) {
                                XqLog.d(WeChatNewsModelImpl.TAG, "--------------------");
                                XqLog.d(WeChatNewsModelImpl.TAG, "title=" + weChatNews.getTitle());
                                XqLog.d(WeChatNewsModelImpl.TAG, "url=" + weChatNews.getUrl());
                                XqLog.d(WeChatNewsModelImpl.TAG, "--------------------");
                            }
                            if(isDown){
                                XqLog.d(WeChatNewsModelImpl.TAG, "下拉数据成功");
                            }else {
                                XqLog.d(WeChatNewsModelImpl.TAG, "上拉数据成功");
                            }
                        } else {
                            if(isDown){
                                XqLog.d(WeChatNewsModelImpl.TAG, "下拉数据失败");
                            }else {
                                XqLog.d(WeChatNewsModelImpl.TAG, "上拉数据失败");
                            }
                        }
                        mView.hideLoadingView(isDown);
                        mView.onGetNewsListSuccess(weChatNewsList,isDown);
                    }

                    @Override
                    public void onError(Throwable e) {
                        XqLog.d(WeChatNewsModelImpl.TAG, "onError出错" + e.getMessage());
                        mView.hideLoadingView(isDown);
                        mView.showError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        XqLog.d(WeChatNewsModelImpl.TAG, "onComplete");
                    }
                });
    }

    @Override
    public Integer getLastNewsRecordPage() {
        return weChatNewsModel.getLastNewsRecordPage();
    }
}
